﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SysNotice
    {
        public int Add(Chain.Model.SysNotice model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysNotice(");
            builder.Append("SysNoticeCode,SysNotieceName,SysNoticeTitle,SysNoticeDetail,SysNoticeTime)");
            builder.Append(" values (");
            builder.Append("@SysNoticeCode,@SysNotieceName,@SysNoticeTitle,@SysNoticeDetail,@SysNoticeTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SysNoticeCode", SqlDbType.VarChar, 50), new SqlParameter("@SysNotieceName", SqlDbType.VarChar, 50), new SqlParameter("@SysNoticeTitle", SqlDbType.VarChar, 500), new SqlParameter("@SysNoticeDetail", SqlDbType.VarChar, 0x1388), new SqlParameter("@SysNoticeTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.SysNoticeCode;
            cmdParms[1].Value = model.SysNotieceName;
            cmdParms[2].Value = model.SysNoticeTitle;
            cmdParms[3].Value = model.SysNoticeDetail;
            cmdParms[4].Value = model.SysNoticeTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int SysNoticeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysNotice ");
            builder.Append(" where SysNoticeID=@SysNoticeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SysNoticeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = SysNoticeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string SysNoticeIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysNotice ");
            builder.Append(" where SysNoticeID in (" + SysNoticeIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int SysNoticeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysNotice");
            builder.Append(" where SysNoticeID=@SysNoticeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SysNoticeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = SysNoticeID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SysNoticeID,SysNoticeCode,SysNotieceName,SysNoticeTitle,SysNoticeDetail,SysNoticeTime ");
            builder.Append(" FROM SysNotice ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" SysNoticeID,SysNoticeCode,SysNotieceName,SysNoticeTitle,SysNoticeDetail,SysNoticeTime ");
            builder.Append(" FROM SysNotice ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.SysNoticeID desc");
            }
            builder.Append(")AS Row, T.*  from SysNotice T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "SysNotice";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "SysNoticeID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("SysNoticeID", "SysNotice");
        }

        public Chain.Model.SysNotice GetModel(int SysNoticeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 SysNoticeID,SysNoticeCode,SysNotieceName,SysNoticeTitle,SysNoticeDetail,SysNoticeTime from SysNotice ");
            builder.Append(" where SysNoticeID=@SysNoticeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SysNoticeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = SysNoticeID;
            Chain.Model.SysNotice notice = new Chain.Model.SysNotice();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["SysNoticeID"] != null) && (set.Tables[0].Rows[0]["SysNoticeID"].ToString() != ""))
            {
                notice.SysNoticeID = int.Parse(set.Tables[0].Rows[0]["SysNoticeID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SysNoticeCode"] != null) && (set.Tables[0].Rows[0]["SysNoticeCode"].ToString() != ""))
            {
                notice.SysNoticeCode = set.Tables[0].Rows[0]["SysNoticeCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SysNotieceName"] != null) && (set.Tables[0].Rows[0]["SysNotieceName"].ToString() != ""))
            {
                notice.SysNotieceName = set.Tables[0].Rows[0]["SysNotieceName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SysNoticeTitle"] != null) && (set.Tables[0].Rows[0]["SysNoticeTitle"].ToString() != ""))
            {
                notice.SysNoticeTitle = set.Tables[0].Rows[0]["SysNoticeTitle"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SysNoticeDetail"] != null) && (set.Tables[0].Rows[0]["SysNoticeDetail"].ToString() != ""))
            {
                notice.SysNoticeDetail = set.Tables[0].Rows[0]["SysNoticeDetail"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SysNoticeTime"] != null) && (set.Tables[0].Rows[0]["SysNoticeTime"].ToString() != ""))
            {
                notice.SysNoticeTime = DateTime.Parse(set.Tables[0].Rows[0]["SysNoticeTime"].ToString());
            }
            return notice;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysNotice ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.SysNotice model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysNotice set ");
            builder.Append("SysNoticeCode=@SysNoticeCode,");
            builder.Append("SysNotieceName=@SysNotieceName,");
            builder.Append("SysNoticeTitle=@SysNoticeTitle,");
            builder.Append("SysNoticeDetail=@SysNoticeDetail,");
            builder.Append("SysNoticeTime=@SysNoticeTime");
            builder.Append(" where SysNoticeID=@SysNoticeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SysNoticeCode", SqlDbType.VarChar, 50), new SqlParameter("@SysNotieceName", SqlDbType.VarChar, 50), new SqlParameter("@SysNoticeTitle", SqlDbType.VarChar, 500), new SqlParameter("@SysNoticeDetail", SqlDbType.VarChar, 0x1388), new SqlParameter("@SysNoticeTime", SqlDbType.DateTime), new SqlParameter("@SysNoticeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.SysNoticeCode;
            cmdParms[1].Value = model.SysNotieceName;
            cmdParms[2].Value = model.SysNoticeTitle;
            cmdParms[3].Value = model.SysNoticeDetail;
            cmdParms[4].Value = model.SysNoticeTime;
            cmdParms[5].Value = model.SysNoticeID;
            if (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0)
            {
                return 1;
            }
            return 0;
        }
    }
}

